home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / machserver / tests / loop / loop.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-03-13  |  4.8 KB  |  224 lines

  1. /* 
  2.  * loop.c --
  3.  *
  4.  *    Simple program to do something interesting forever.  One use is to
  5.  *    help track down memory leaks in the Sprite server.  Another is to
  6.  *    test signals support.
  7.  *
  8.  * Copyright 1991 Regents of the University of California
  9.  * Permission to use, copy, modify, and distribute this
  10.  * software and its documentation for any purpose and without
  11.  * fee is hereby granted, provided that this copyright
  12.  * notice appears in all copies.  The University of California
  13.  * makes no representations about the suitability of this
  14.  * software for any purpose.  It is provided "as is" without
  15.  * express or implied warranty.
  16.  */
  17.  
  18. #ifndef lint
  19. static char rcsid[] = "$Header: /user5/kupfer/spriteserver/tests/loop/RCS/loop.c,v 1.2 92/03/12 20:49:02 kupfer Exp $ SPRITE (Berkeley)";
  20. #endif /* not lint */
  21.  
  22. #include <signal.h>
  23. #include <setjmp.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <sys/time.h>
  27. #include <sys/types.h>
  28. #include <test.h>
  29. #include <time.h>
  30. #include <unistd.h>
  31.  
  32. /* 
  33.  * If sigpause is used instead of sleep, there are two ways to go.  One is 
  34.  * to set a one-time alarm each time through the loop.  The other is to set 
  35.  * up a periodic timer exactly once.  If this flag is defined, then a 
  36.  * periodic timer is used.
  37.  */
  38. Boolean usePeriodTimer = FALSE;
  39.  
  40. int sleepSecs = 1;        /* number of seconds to pause */
  41. int ignoreSigint = 0;        /* block SIGINT if non-zero */
  42. int sigintFlag = 0;        /* did we get a SIGINT */
  43. int useSigPause = 0;        /* use alarm/sigpause instead of sleep */
  44.  
  45. jmp_buf jmpBuf;            /* setjmp state */
  46.  
  47. #define NO_HANDLER    0
  48. #define SET_FLAG    1    /* handler just sets a flag */
  49. #define DO_LONGJMP    2    /* handler does a longjmp */
  50. int useHandler = NO_HANDLER;    /* register a handler for SIGINT? */
  51.  
  52. void FlagHandler();
  53. void LongjmpHandler();
  54. void CatchAlarm();
  55.  
  56.  
  57. /*
  58.  *----------------------------------------------------------------------
  59.  *
  60.  * main --
  61.  *
  62.  *    In an infinite loop, print the time and sleep a bit.
  63.  *
  64.  * Results:
  65.  *    None.
  66.  *
  67.  * Side effects:
  68.  *    None.
  69.  *
  70.  *----------------------------------------------------------------------
  71.  */
  72.  
  73. int
  74. main(argc, argv)
  75.     int argc;
  76.     char *argv[];
  77. {
  78.     time_t now;
  79.     int argChar;
  80.     extern char *optarg;
  81.     char *usageString = "usage: loop [-i|-h type] [-p [-t]] [-s seconds]\n";
  82.     struct itimerval timer;    /* interval timer */
  83.  
  84.     /* 
  85.      * Parse the command line.
  86.      */
  87.     while ((argChar = getopt(argc, argv, "s:ih:pt")) != EOF) {
  88.     switch (argChar) {
  89.     case 'i':
  90.         ignoreSigint = 1;
  91.         break;
  92.     case 'h':
  93.         switch (*optarg) {
  94.         case 'f':
  95.         useHandler = SET_FLAG;
  96.         break;
  97.         case 'l':
  98.         useHandler = DO_LONGJMP;
  99.         break;
  100.         default:
  101.         Test_PutMessage("handler type is 'f' or 'l'.\n");
  102.         exit(1);
  103.         break;
  104.         }
  105.         break;
  106.     case 'p':
  107.         useSigPause = 1;
  108.         break;
  109.     case 's':
  110.         sleepSecs = atoi(optarg);
  111.         break;
  112.     case 't':
  113.         usePeriodTimer = TRUE;
  114.         break;
  115.     default:
  116.         Test_PutMessage(usageString);
  117.         exit(1);
  118.         break;
  119.     }
  120.     }
  121.     if (ignoreSigint && useHandler != NO_HANDLER) {
  122.     Test_PutMessage(usageString);
  123.     exit(1);
  124.     }
  125.  
  126.     /* 
  127.      * Ignore interrupts if requested.
  128.      */
  129.     if (ignoreSigint) {
  130.     if (signal(SIGINT, SIG_IGN) == BADSIG) {
  131.         perror("can't ignore SIGINT");
  132.         exit(1);
  133.     }
  134.     } else {
  135.     switch (useHandler) {
  136.     case SET_FLAG:
  137.         if (signal(SIGINT, FlagHandler) == BADSIG) {
  138.         perror("can't register FlagHandler");
  139.         exit(1);
  140.         }
  141.         break;
  142.     case DO_LONGJMP:
  143.         if (signal(SIGINT, LongjmpHandler) == BADSIG) {
  144.         perror("can't register LongjmpHandler");
  145.         exit(1);
  146.         }
  147.         break;
  148.     }
  149.     }
  150.  
  151.     if (setjmp(jmpBuf)) {
  152.     Test_PutMessage("\nlongjmp\n");
  153.     }
  154.  
  155.     if (useSigPause) {
  156.     if (signal(SIGALRM, CatchAlarm) == BADSIG) {
  157.         perror("can't register alarm handler");
  158.         exit(1);
  159.     }
  160.     if (usePeriodTimer) {
  161.         timer.it_interval.tv_sec = sleepSecs;
  162.         timer.it_interval.tv_usec = 0;
  163.         timer.it_value = timer.it_interval;
  164.         if (setitimer(ITIMER_REAL, &timer, (struct itimerval *)0) < 0) {
  165.         perror("Can't set interval timer");
  166.         exit(1);
  167.         }
  168.     }
  169.     }
  170.  
  171.     /* 
  172.      * Loop, printing the time every so often.
  173.      */
  174.     if (sleepSecs <= 0) {
  175.     Test_PutMessage("Going into infinite loop.\n");
  176.     }
  177.     for (;;) {
  178.     if (sleepSecs > 0) {
  179.         now = time(0);
  180.         Test_PutTime(now, TRUE);
  181.         if (useSigPause) {
  182.         if (!usePeriodTimer) {
  183.             alarm(sleepSecs);
  184.         }
  185.         (void)sigpause(0);
  186.         } else {
  187.         sleep(sleepSecs);
  188.         }
  189.     }
  190.     if (sigintFlag) {
  191.         Test_PutMessage("\nsigint\n");
  192.         sigintFlag = 0;
  193.     }
  194.     }
  195. }
  196.  
  197. void
  198. FlagHandler(sigNum, code, contextPtr, addr)
  199.     int sigNum, code;
  200.     char *contextPtr, *addr;
  201. {
  202. #ifdef lint
  203.     sigNum = sigNum;
  204.     code = code;
  205.     contextPtr = contextPtr;
  206.     addr = addr;
  207. #endif
  208. #if 0
  209.     printf("\n(%d, %d, 0x%x, 0x%x)\n", sigNum, code, contextPtr, addr);
  210. #endif
  211.     sigintFlag = 1;
  212. }
  213.  
  214. void
  215. LongjmpHandler()
  216. {
  217.     longjmp(jmpBuf, 1);
  218. }
  219.  
  220. void
  221. CatchAlarm()
  222. {
  223. }
  224.